package com.xydtech.ecard.merchant.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.ecard.merchant.domain.EcardPriorCreditChart;
import com.xydtech.ecard.merchant.domain.EcardPriorCreditDept;
import com.xydtech.ecard.merchant.domain.EcardPriorCreditUser;
import com.xydtech.ecard.merchant.mapper.EcardPriorCreditChartMapper;
import com.xydtech.ecard.merchant.service.IEcardPriorCreditChartService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 预授信图表统计Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-05-27 下午4:58
 */
@Service
public class EcardPriorCreditChartServiceImpl implements IEcardPriorCreditChartService {

    @Autowired
    private EcardPriorCreditChartMapper ecardPriorCreditChartMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 个人预授信额度统计信息
     * @param userId
     * @return
     */
    @Override
    public JSONObject getPriorCreditLimitStatistics(Long userId) {
        JSONObject jsonObject = new JSONObject();
        EcardPriorCreditChart priorCreditChart = new EcardPriorCreditChart();
        EcardPriorCreditChart ecardPriorCreditChart = ecardPriorCreditChartMapper.getPriorCreditLimitStatistics(userId);
        if (ecardPriorCreditChart == null) {
            priorCreditChart.setPriorCreditLimit(0f);
            priorCreditChart.setAnthropoidsLimit(0f);
            priorCreditChart.setMerchantLimit(0f);
            priorCreditChart.setThisMouthPriorCreditLimit(0f);
            priorCreditChart.setLastMouthPriorCreditLimit(0f);
            priorCreditChart.setComparePriorCreditLimit(0f);
            jsonObject.put("priorCreditStatistics", priorCreditChart);
        } else {
            jsonObject.put("priorCreditStatistics", ecardPriorCreditChart);
        }
        return jsonObject;
    }

    /**
     * 个人预授信户数统计信息
     * @param userId
     * @return
     */
    @Override
    public JSONObject getPriorCreditNumStatistics(Long userId) {
        JSONObject jsonObject = new JSONObject();
        EcardPriorCreditChart priorCreditChart = new EcardPriorCreditChart();
        EcardPriorCreditChart ecardPriorCreditChart = ecardPriorCreditChartMapper.getPriorCreditNumStatistics(userId);
        if (ecardPriorCreditChart == null) {
            priorCreditChart.setPriorCreditNum(0L);
            priorCreditChart.setAnthropoidsNum(0L);
            priorCreditChart.setMerchantNum(0L);
            priorCreditChart.setThisMouthPriorCreditNum(0L);
            priorCreditChart.setLastMouthPriorCreditNum(0L);
            priorCreditChart.setComparePriorCreditNum(0L);
            jsonObject.put("priorCreditNumStatistics", priorCreditChart);
        } else {
            jsonObject.put("priorCreditNumStatistics", ecardPriorCreditChart);
        }
        return jsonObject;
    }

    /**
     * 全行预授信额度统计信息
     * @param deptId
     * @return
     */
    @Override
    public JSONObject getAllPriorCreditLimit(Long deptId) {
        JSONObject jsonObject = new JSONObject();
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        EcardPriorCreditChart priorCreditChart = new EcardPriorCreditChart();
        EcardPriorCreditChart ecardPriorCreditChart = ecardPriorCreditChartMapper.getAllPriorCreditLimit(sysDept.getDeptId());
        if (ecardPriorCreditChart == null) {
            priorCreditChart.setPriorCreditLimit(0f);
            priorCreditChart.setAnthropoidsLimit(0f);
            priorCreditChart.setMerchantLimit(0f);
            priorCreditChart.setThisMouthPriorCreditLimit(0f);
            priorCreditChart.setLastMouthPriorCreditLimit(0f);
            priorCreditChart.setComparePriorCreditLimit(0f);
            jsonObject.put("allPriorCreditLimit", priorCreditChart);
        } else {
            jsonObject.put("allPriorCreditLimit", ecardPriorCreditChart);
        }
        return jsonObject;
    }

    /**
     * 全行预授信户数统计信息
     * @param deptId
     * @return
     */
    @Override
    public JSONObject getAllPriorCreditNum(Long deptId) {
        JSONObject jsonObject = new JSONObject();
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        EcardPriorCreditChart priorCreditChart = new EcardPriorCreditChart();
        EcardPriorCreditChart ecardPriorCreditChart = ecardPriorCreditChartMapper.getAllPriorCreditNum(sysDept.getDeptId());
        if (ecardPriorCreditChart == null) {
            priorCreditChart.setPriorCreditNum(0L);
            priorCreditChart.setAnthropoidsNum(0L);
            priorCreditChart.setMerchantNum(0L);
            priorCreditChart.setThisMouthPriorCreditNum(0L);
            priorCreditChart.setLastMouthPriorCreditNum(0L);
            priorCreditChart.setComparePriorCreditNum(0L);
            jsonObject.put("allPriorCreditNum", priorCreditChart);
        } else {
            jsonObject.put("allPriorCreditNum", ecardPriorCreditChart);
        }
        return jsonObject;
    }

    /**
     * 查询额度折线图数据
     * @param deptId
     * @return
     */
    @Override
    public List<EcardPriorCreditChart> getLimitLineChart(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardPriorCreditChartMapper.getLimitLineChart(sysDept.getDeptId());
    }

    /**
     * 查询额度折线图数据（小程序接口）
     * @param deptId
     * @return
     */
    @Override
    public List<EcardPriorCreditChart> getLimitLineChartByApplet(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardPriorCreditChartMapper.getLimitLineChartByApplet(sysDept.getDeptId());
    }

    /**
     * 查询户数折线图数据
     * @param deptId
     * @return
     */
    @Override
    public List<EcardPriorCreditChart> getNumLineChart(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardPriorCreditChartMapper.getNumLineChart(sysDept.getDeptId());
    }

    /**
     * 查询户数柱状图数据（小程序接口）
     * @param deptId
     * @return
     */
    @Override
    public List<EcardPriorCreditChart> getNumLineChartByApplet(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardPriorCreditChartMapper.getNumLineChartByApplet(sysDept.getDeptId());
    }

    /**
     * 各机构预授信业务排名
     * @param ecardPriorCreditDept
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardPriorCreditDept> getDeptPriorCreditList(EcardPriorCreditDept ecardPriorCreditDept) {
        return ecardPriorCreditChartMapper.getDeptPriorCreditList(ecardPriorCreditDept);
    }

    /**
     * 各营销人员预授信业务排名
     * @param ecardPriorCreditUser
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<EcardPriorCreditUser> getUserPriorCreditList(EcardPriorCreditUser ecardPriorCreditUser) {
        return ecardPriorCreditChartMapper.getUserPriorCreditList(ecardPriorCreditUser);
    }

    /**
     * 各机构预授信业务排名（小程序接口）
     * @param ecardPriorCreditDept
     * @return
     */
    @Override
    public List<EcardPriorCreditDept> getDeptPriorCreditListByApplet(EcardPriorCreditDept ecardPriorCreditDept) {
        return ecardPriorCreditChartMapper.getDeptPriorCreditListByApplet(ecardPriorCreditDept);
    }

    /**
     * 各营销人员预授信业务排名（小程序接口）
     * @param ecardPriorCreditUser
     * @return
     */
    @Override
    public List<EcardPriorCreditUser> getUserPriorCreditListByApplet(EcardPriorCreditUser ecardPriorCreditUser) {
        return ecardPriorCreditChartMapper.getUserPriorCreditListByApplet(ecardPriorCreditUser);
    }
}
